﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace TileMapEditor.Internal
{
    [Serializable]
    internal class SpriteRectCache : ScriptableObject
    {
        [SerializeField]
        public List<SpriteRect> rects;

        public int Count
        {
            get
            {
                int result;
                if ( this.rects != null )
                {
                    result = this.rects.Count;
                }
                else
                {
                    result = 0;
                }
                return result;
            }
        }

        public SpriteRect RectAt( int i )
        {
            SpriteRect result;
            if ( i >= this.Count )
            {
                result = null;
            }
            else
            {
                result = this.rects[i];
            }
            return result;
        }

        public void AddRect( SpriteRect r )
        {
            if ( this.rects != null )
            {
                this.rects.Add( r );
            }
        }

        public void RemoveRect( SpriteRect r )
        {
            if ( this.rects != null )
            {
                this.rects.Remove( r );
            }
        }

        public void ClearAll()
        {
            if ( this.rects != null )
            {
                this.rects.Clear();
            }
        }

        public int GetIndex( SpriteRect spriteRect )
        {
            int result;
            if ( this.rects != null )
            {
                result = this.rects.FindIndex( ( SpriteRect p ) => p.Equals( spriteRect ) );
            }
            else
            {
                result = 0;
            }
            return result;
        }

        public bool Contains( SpriteRect spriteRect )
        {
            return this.rects != null && this.rects.Contains( spriteRect );
        }

        private void OnEnable()
        {
            if ( this.rects == null )
            {
                this.rects = new List<SpriteRect>();
            }
        }
    }
}